#ifndef __MY_HOT__
#define __MY_HOT__
#include"data.hpp"
#include<unistd.h>
extern cloud::DataManager* _data;

namespace cloud
{
    class HotManager
    {
    private:
        bool CheckHot(const std::string& filename)
        {
            FileUtil file(filename);
            time_t atime=file.LastATime();
            time_t current_time=time(nullptr);

            if(current_time-atime>_hot_time)
            {
                return false;
            }

            return true;
        }
    public:

        HotManager()
        {
            Config* config=Config::GetInstance();
            _back_dir=config->GetBackDir();
            _pack_dir=config->GetPackDir();
            _pack_suffix=config->GetPackSuffix();
            _hot_time=config->GetHotTime();

            FileUtil file1(_pack_dir);
            FileUtil file2(_back_dir);

            file1.CreateDirectory();
            file2.CreateDirectory();

        }
        bool RunModul()
        {
            
            FileUtil file(_back_dir);

            while(1)
            {
                std::vector<std::string> arr;
                file.ScanDirectory(&arr);
                

                for (int i = 0; i < arr.size(); i++)
                {
                    
                    if (CheckHot(arr[i]))
                    {
                        continue;
                    }

                    FileUtil tmp(arr[i]);
                    BackupInfo info;
                    if (_data->GetOneByRealPath(arr[i], &info) == false)
                    {
                        info.NewBackupInfo(arr[i]);
                        _data->Insert(info);
                    }

                    tmp.Compress(info.pack_path);
                    tmp.Remove();
                    info.pack_flag=true;
                    _data->Update(info);
                }

                usleep(100000);
            }

            return true;
        }
    private:
        std::string _back_dir;
        std::string _pack_dir;
        std::string _pack_suffix;
        int _hot_time;
    };
}

#endif